<script>
export default {
  onLaunch() {
    console.log('App Launch')
    // 检查更新
    this.checkUpdate()
    // 获取用户信息
    this.getUserInfo()
  },
  onShow() {
    console.log('App Show')
    // 检查登录状态
    this.checkLogin()
  },
  onHide() {
    console.log('App Hide')
  },
  methods: {
    // 检查小程序更新
    checkUpdate() {
      if (uni.canIUse('getUpdateManager')) {
        const updateManager = uni.getUpdateManager()
        updateManager.onCheckForUpdate((res) => {
          if (res.hasUpdate) {
            updateManager.onUpdateReady(() => {
              uni.showModal({
                title: '更新提示',
                content: '新版本已经准备好，是否重启应用？',
                success: (res) => {
                  if (res.confirm) {
                    updateManager.applyUpdate()
                  }
                }
              })
            })
          }
        })
      }
    },

    // 检查登录状态
    checkLogin() {
      const token = uni.getStorageSync('token')
      if (!token) {
        const currentPages = getCurrentPages()
        const currentPage = currentPages[currentPages.length - 1]
        const noAuthPages = ['pages/login/index', 'pages/register/index']
        if (!noAuthPages.includes(currentPage?.route)) {
          uni.navigateTo({
            url: '/pages/login/index'
          })
        }
      }
    },

    // 获取用户信息
    async getUserInfo() {
      try {
        const token = uni.getStorageSync('token')
        if (token) {
          const userInfo = await this.$store.dispatch('getUserInfo')
          if (!userInfo) {
            // token 失效，清除存储并跳转登录页
            uni.removeStorageSync('token')
            uni.removeStorageSync('userInfo')
            this.checkLogin()
          }
        }
      } catch (error) {
        console.error('获取用户信息失败', error)
      }
    }
  }
}
</script>

<style lang="scss">
/* 全局样式 */
page {
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica,
    Segoe UI, Arial, Roboto, 'PingFang SC', 'miui', 'Hiragino Sans GB', 'Microsoft Yahei',
    sans-serif;
  background-color: #f5f5f5;
}

/* 去除按钮默认边框 */
button {
  padding: 0;
  margin: 0;
  background: none;
  border: none;
  border-radius: 0;
  outline: none;
  appearance: none;
  
  &::after {
    border: none;
  }
}

/* 去除导航栏边框 */
.uni-navbar--border {
  border-bottom: none !important;
}

/* 通用容器样式 */
.container {
  min-height: 100vh;
  box-sizing: border-box;
}

/* 通用卡片样式 */
.card {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

/* 通用按钮样式 */
.btn {
  height: 88rpx;
  line-height: 88rpx;
  text-align: center;
  border-radius: 44rpx;
  font-size: 32rpx;
  
  &.primary {
    background: #07c160;
    color: #fff;
  }
  
  &.default {
    background: #f5f5f5;
    color: #333;
  }
  
  &.warn {
    background: #fff3f3;
    color: #f44336;
  }
  
  &[disabled] {
    opacity: 0.6;
  }
}

/* 1px 边框解决方案 */
.border-top,
.border-bottom {
  position: relative;
  
  &::after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    height: 1px;
    background: #eee;
    transform: scaleY(0.5);
  }
}

.border-top::after {
  top: 0;
}

.border-bottom::after {
  bottom: 0;
}

/* 文本省略 */
.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ellipsis-2 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

/* 通用间距 */
.mt-10 { margin-top: 10rpx; }
.mt-20 { margin-top: 20rpx; }
.mt-30 { margin-top: 30rpx; }
.mb-10 { margin-bottom: 10rpx; }
.mb-20 { margin-bottom: 20rpx; }
.mb-30 { margin-bottom: 30rpx; }
.ml-10 { margin-left: 10rpx; }
.ml-20 { margin-left: 20rpx; }
.ml-30 { margin-left: 30rpx; }
.mr-10 { margin-right: 10rpx; }
.mr-20 { margin-right: 20rpx; }
.mr-30 { margin-right: 30rpx; }

/* 通用颜色 */
.text-primary { color: #07c160; }
.text-warning { color: #ff9800; }
.text-danger { color: #f44336; }
.text-info { color: #2196f3; }
.text-success { color: #4caf50; }
.text-muted { color: #999; }

/* 通用字体大小 */
.font-sm { font-size: 24rpx; }
.font-md { font-size: 28rpx; }
.font-lg { font-size: 32rpx; }
.font-xl { font-size: 36rpx; }

/* 通用flex布局 */
.flex {
  display: flex;
}

.flex-column {
  display: flex;
  flex-direction: column;
}

.flex-1 {
  flex: 1;
}

.justify-between {
  justify-content: space-between;
}

.justify-center {
  justify-content: center;
}

.align-center {
  align-items: center;
}

/* 通用阴影 */
.shadow {
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
}

/* 通用动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}

/* 安全区适配 */
.safe-area-bottom {
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}

.safe-area-top {
  padding-top: constant(safe-area-inset-top);
  padding-top: env(safe-area-inset-top);
}
</style> 